﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Trade.Model;
using System.Windows.Forms;

namespace Trade.DAL
{
    public class User
    {
        //用户登录校验
        public static UserInfo ValidateLogin(string userId, string pwd)
        {
            UserInfo curUser = null;
            string cmdString = "Select * from UserData where UserId = @UserId and Pwd = @Pwd";

            SqlParameter[] parms = {
                new SqlParameter("@UserId", SqlDbType.VarChar, 15),
                new SqlParameter("@Pwd", SqlDbType.VarChar, 15)};

            parms[0].Value = userId;
            parms[1].Value = pwd;

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.connectionString, CommandType.Text, cmdString, parms))
            {
                if (rdr.Read())
                {
                    curUser = new UserInfo(rdr.GetString(0),rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetDateTime(4),
                                           rdr.GetDateTime(5), rdr.GetBoolean(6),rdr.GetBoolean(7));
                }
            }
                return curUser;
        }

        //设置密码
        public static bool SetNewPassword(string userId, string pwd)
        {
            string cmdString = "Update UserData set Pwd = ltrim(rtrim(@Pwd)) where ltrim(rtrim(UserId)) = @UserId";

            SqlParameter[] parms = {
                new SqlParameter("@UserId", SqlDbType.VarChar, 15),
                new SqlParameter("@Pwd", SqlDbType.VarChar, 15)};

            parms[0].Value = userId;
            parms[1].Value = pwd;

            int val = SQLHelper.ExecuteNonQuery(SQLHelper.connectionString, CommandType.Text, cmdString, parms);

            if (val > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //用户修改密码
        public static bool ChangePassword(string userId, string oldPwd, string newPwd)
        {
            string cmdString = "Update UserData set Pwd = ltrim(rtrim(@NewPwd)) where ltrim(rtrim(UserId)) = @UserId and ltrim(rtrim(Pwd)) = @OldPwd";

            SqlParameter[] parms = {
                new SqlParameter("@UserId", SqlDbType.VarChar, 15),
                new SqlParameter("@OldPwd", SqlDbType.VarChar, 15),
                new SqlParameter("@NewPwd", SqlDbType.VarChar, 15)};

            parms[0].Value = userId;
            parms[1].Value = oldPwd;
            parms[2].Value = newPwd;

            int val = SQLHelper.ExecuteNonQuery(SQLHelper.connectionString, CommandType.Text, cmdString, parms);

            if (val > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //创建新用户
        public static bool NewUser(UserInfo userData)
        {
            string cmdString = "Insert into UserData (UserId, UserName, UserDes, Pwd, StartDate, EndDate, LockFlag, AdminFlag) Values (@UserId, @UserName, @UserDes, @Pwd, @StartDate, @EndDate, @LockFlag, @AdminFlag)";

            SqlParameter[] parms = {
                new SqlParameter("@UserId", SqlDbType.VarChar, 15),
                new SqlParameter("@UserName", SqlDbType.VarChar, 16),
                new SqlParameter("@UserDes", SqlDbType.VarChar, 20), 
                new SqlParameter("@Pwd", SqlDbType.VarChar, 15), 
                new SqlParameter("@StartDate", SqlDbType.DateTime), 
                new SqlParameter("@EndDate", SqlDbType.DateTime),
                new SqlParameter("@LockFlag", SqlDbType.Bit),
                new SqlParameter("@AdminFlag", SqlDbType.Bit)};

            parms[0].Value = userData.UserId.Trim();
            parms[1].Value = userData.UserName.Trim();
            parms[2].Value = userData.UserDes.Trim();
            parms[3].Value = userData.Pwd.Trim();
            parms[4].Value = userData.StartDate;
            parms[5].Value = userData.EndDate;
            parms[6].Value = userData.LockFlag;
            parms[7].Value = userData.AdminFlag;

            int val = SQLHelper.ExecuteNonQuery(SQLHelper.connectionString, CommandType.Text, cmdString, parms);

            if (val > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //取得用户菜单权限
        public static void GetUserPermission(string userId, MenuStrip menuStrip)
        {
            string cmdString = "Select * from UserPermission where ltrim(rtrim(UserId)) = @UserId";

            SqlParameter parm = new SqlParameter("@UserId", SqlDbType.VarChar, 15);
            parm.Value = userId;

            string name = null;

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.connectionString,CommandType.Text, cmdString, parm))
            {
                while (rdr.Read())
                {
                    //取得ToolStripMenuItem的Name
                    name = rdr.GetString(1).Trim();
                    
                    foreach (ToolStripMenuItem mi in menuStrip.Items)
                    {
                        if (mi.Name == name)
                        {
                            mi.Enabled = true;
                        }

                        SetEnabledDropDownItems(mi, name);
                    }
                }
            }
        }

        static void SetEnabledDropDownItems(ToolStripMenuItem ctrl, string name)
        {
            if (ctrl.DropDownItems.Count != 0)
            {
                for (int i = 0; i < ctrl.DropDownItems.Count; i++)
                {
                    if (ctrl.DropDownItems[i] is ToolStripMenuItem)
                    {
                        if (ctrl.DropDownItems[i].Name == name)
                        {
                            ctrl.DropDownItems[i].Enabled = true;
                        }

                        SetEnabledDropDownItems((ToolStripMenuItem)ctrl.DropDownItems[i], name);
                    }
                }
            }

        }
    }
}
